package opensocial.apps.treat.model
{
  import opensocial.apps.treat.events.TreatDataRequest;
  
  import flash.events.EventDispatcher;
  
  import mx.collections.ArrayCollection;
  import mx.controls.Alert;
  import mx.core.Application;
  
  public class Utils extends EventDispatcher
  {
    public static function getMaster():User {
      return Application.application.mainCanvas.master;
    }
    
    public static function getFriend():User {
      return Application.application.mainCanvas.friend;
    }
    
    public static function getPlace():Place {
      return Application.application.mainCanvas.place;
    }

    public static function getFriends():ArrayCollection {
      return Application.application.mainCanvas.friends;
    }

    public static function getDataRequest():TreatDataRequest {
      return Application.application.mainCanvas.dataRequest;
    }
    
    public static function getStatus(index:Number):String {
      var status:String = '';
      switch (index) {
        case 0: 
          status = '空闲';
          break;
        case 1: 
          status = '参加活动';        
          break;
      }
      return status;
    }

    public static function getUserTheme(index:Number):String {
      var fileName:String;
      switch (index) {
        case 1: 
          fileName = 'bg01.png';
          break;
        case 2: 
          fileName = 'bg02.png';
          break;
        case 3: 
          fileName = 'bg03.png';
         break;
        case 4: 
          fileName = 'bg04.png';
          break;
        case 5: 
          fileName = 'bg05.png';
          break;
        case 6: 
          fileName = 'bg06.png';
          break;
      }
      return Constants.WWW_URL_BASE + fileName;      
    }
    
    public static function getPlaces():ArrayCollection {
      return Application.application.mainCanvas.places;
    }

    public static function getPlaceTheme(index:Number):String {
      var fileName:String;
      switch (index) {
        case 1: 
          fileName = 'restaurant01.png';
          break;
        case 2: 
          fileName = 'restaurant02.png';
          break;
        case 3: 
          fileName = 'restaurant03.png';
         break;
        case 4: 
          fileName = 'restaurant04.png';
          break;
        case 5: 
          fileName = 'restaurant05.png';
          break;
        case 6: 
          fileName = 'restaurant06.png';
          break;
        case 7: 
          fileName = 'restaurant07.png';
          break;
        case 8: 
          fileName = 'restaurant08.png';
          break;
      }
      return Constants.WWW_URL_BASE + fileName;    
    }
    
    /*
     * @descript: 通过ID从集合中获取该ID的记录
     */
    public static function getInstance(id: Object, collection: ArrayCollection): Object {
    	for each (var obj: Object in collection)
    		if (obj.id == id)
    			return obj;
    	return null;
    }
    
    
    /*
     * @descript: 用户某一时间是否有安排
     */
//    public static function isPlan (u: User, i: Invitation): Boolean {
//    	for each (var inv: Invitation in Constants.INVITATIONS)
//    		if ((inv.accept.contains(u) || inv.uninvited.contains(u) || inv.master == u) &&
//    				((i.startTime.getTime() >= inv.startTime.getTime() && 
//    				i.startTime.getTime() <= inv.endTime.getTime()) ||
//    				(i.endTime.getTime() >= inv.startTime.getTime() && 
//    				i.endTime.getTime() <= inv.endTime.getTime())))
//    			return false;
//    	return true;
//    }

    
    /*
     * @descript: 把本地存储中取出来的序列化后的邀请对象转换成一个邀请对象
     */
//    public static function newInstanceInvitation(o: Object): Invitation {
//    	var tGursts: ArrayCollection = new ArrayCollection();
//    	for each (var obj5: Object in o.guests)
//    		tGursts.addItem(User(getInstance(obj5.id, Constants.PERSONS)));
//    	var newInvi: Invitation = new Invitation(o.id, 
//    			User(getInstance(o.master.id, Constants.PERSONS)), tGursts, 
//    			o.startTime, Place(getInstance(o.place.id, Constants.PLACES)));
//    	newInvi.status = o.status;
//    	for each (var obj1: Object in o.accept)
//    		newInvi.accept.addItem(User(getInstance(obj1.id, Constants.PERSONS)));
//    	for each (var obj2: Object in o.uninvited)
//    		newInvi.uninvited.addItem(User(getInstance(obj2.id, Constants.PERSONS)));
//    	for each (var obj3: Object in o.declined)
//    		newInvi.declined.addItem(User(getInstance(obj3.id, Constants.PERSONS)));
//    	tGursts = new ArrayCollection();
//    	for each (var obj4: Object in o.unresponsive) {
//    		tGursts.addItem(User(getInstance(obj4.id, Constants.PERSONS)));
//    	}
//			newInvi.unresponsive = tGursts;
//    	return newInvi;
//    }
    
    public function Utils() {
    }


    public static function getPlaceById(id:Number):Place {
      var dataRequest:TreatDataRequest = getDataRequest();
      var places:ArrayCollection = dataRequest.places;
      if (places) {
        for each (var p:Place in places) {
          if (p.id == id) {
            return p;
          }          
        }        
      }
      return null;
    }
    
    public static function getFriendById(id:String):User {
      var dataRequest:TreatDataRequest = getDataRequest();
      var friends:ArrayCollection = dataRequest.friends;
      if (friends) {
        for each (var u:User in friends) {
          if (u.id == id) {
            return u;
          }
        }        
      }
      return null;
    }
  }
}